package cn.flashk.controls {
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.utils.setTimeout;
	
	import cn.flashk.controls.events.TabBarEvent;
	import cn.flashk.controls.managers.SkinLoader;
	import cn.flashk.controls.managers.SkinManager;
	import cn.flashk.controls.managers.SourceSkinLinkDefine;
	import cn.flashk.controls.managers.StyleManager;
	import cn.flashk.controls.skin.ActionDrawSkin;
	import cn.flashk.controls.skin.TabBarSkin;
	import cn.flashk.controls.skin.sourceSkin.NavSourceSkin;
	import cn.flashk.controls.support.Nav;
	import cn.flashk.controls.support.UIComponent;

	/**
	 * tab切换
	 * eventType cn.flashk.controls.events.TabBarEvent 
	 */	
	[Event(name="tab_switch",type="cn.flashk.controls.events.TabBarEvent")]
	
	public class TabNav extends UIComponent {
		public static var tabHeight:Number = 25;
		/**选中时偏移*/
		public static var selectUp:Number = 2;
		
		/**自动裁剪*/
		public var isAutoClip:Boolean = true;
		//切换操作的最少间隔时间:毫秒
		public var ableClickAfterTime:uint = 20;
		protected var _padding:Number = 0;
		protected var navs:Array;
		protected var _paddingTop:Number = 28;
		
		protected var isHide:Boolean;
		protected var _isAutoResetXY:Boolean = true;
		protected var switchFun:Function;
		protected var index:uint = 0;
		protected var hn:Number = 1;
		protected var sn:Number = 1;
		protected var _contents:Array;
		protected var _labels:Array;
		protected var box:Sprite;
		protected var nowContent:DisplayObject;
		
		public function TabNav(skinID:String="") {
			super(skinID);
			isAutoClip = StyleManager.globalTabbarAutoClip;
			_compoWidth = 264;
			_compoHeight = 280;
			navs = [];
			_contents = [];
			box = new Sprite();
			box.y = _paddingTop;
			_padding = 2;
			styleSet["padding"] = _padding;
			this.addChild(box);
			
			setSize(_compoWidth, _compoHeight);
		}
		
		public function set padding(value:Number):void {
			_padding = value;
		}
		
		public function set paddingTop(value:Number):void {
			_paddingTop = value;
			if (box != null){
				box.y = _paddingTop;
			}
		}
		
		public function get container():Sprite {
			return box;
		}
		
		/**
		 * 根据padding布局容器
		 * @return 
		 */		
		public function get isAutoResetXY():Boolean {
			return _isAutoResetXY;
		}
		
		public function set isAutoResetXY(value:Boolean):void {
			_isAutoResetXY = value;
		}
		
		public function get contents():Array {
			return _contents;
		}
		
		/**
		 * 标签切换淡出的Alpha减少的值。如果内容区域过大，建议将此值设为1以关闭alpha过渡
		 */
		public function set alphaHideNum(value:Number):void {
			hn = value;
		}
		
		/**
		 * 标签切换淡入的Alpha增加的值。如果内容区域过大，建议将此值设为1以关闭alpha过渡
		 */
		public function set alphaShowNum(value:Number):void {
			sn = value;
		}
		
		/**
		 * 获得对某个索引处的Tab
		 * @param index
		 * @return
		 */
		public function getNavAt(index:uint):Nav {
			return navs[index] as Nav;
		}
		
		/**
		 * 设置用户是否可以点击某个选项卡
		 * @param index
		 * @param isAble 是否禁止，默认false（禁止）
		 *
		 */
		public function setAbleAt(index:uint, isAble:Boolean = false):void {
			getNavAt(index).enabled = isAble;
		}
		
		/**
		 * 宽度设置整除子项的个数
		 * 向TabBar动态添加一个tab
		 * 
		 * @param tabName 标签要显示的文本
		 * @param content 标签的显示内容
		 */
		public function addTab(navName:String, content:DisplayObject=null):Nav {
			var nav:Nav;
			if (content == null)
				content = new Sprite();
			nav = new Nav(_skinID);
			nav.index = navs.length;
			nav.label = navName;
			nav.btn.addEventListener(MouseEvent.CLICK, switchToTabClick);
			navs[navs.length] = nav;

			if (SkinManager.isUseDefaultSkin == true){
				this.addChildAt(nav, 1);
			} else {
				this.addChild(nav);
			}
			_contents[navs.length - 1] = content;
			if (content.parent != null)
				content.parent.removeChild(content);
			
			alignTabs();
			return nav;
		}
		
		private function ableClick():void {
			this.mouseChildren = true;
		}
		
		public function alphaMotionOff():void {
			hn = 1;
			sn = 1;
		}
		
		public function alphaMotionOn():void {
			hn = 0.07;
			sn = 0.12
		}
		
		override public function setDefaultSkin():void {
			setSkin(TabBarSkin);
		}
		
		override public function setSourceSkin():void {
			setSkin(SkinLoader.getClassFromSkinFile(SourceSkinLinkDefine.TAB_BACKGROUND));
		}
		
		override public function setLoaderSkin(skinID:String=""):void{
			if(skinID!=""){
				_skinID = skinID;
			}
			var sous:NavSourceSkin = new NavSourceSkin(_skinID);
			sous.init(this, styleSet, null);
			skin = sous;
		}
		
		override public function setSkin(Skin:Class):void {
			if (SkinManager.isUseDefaultSkin == true){
				skin = new Skin();
				TabBarSkin(skin).init(this, styleSet);
			} else {
				var sous:NavSourceSkin = new NavSourceSkin();
				sous.init(this, styleSet, Skin);
				skin = sous;
			}
		}
		
		/**
		 * 选中的状态 
		 * 显示当前索引的容器
		 * @param index
		 * 
		 */		
		protected function viewContentByIndex(index:uint):void {
			if (nowContent != null && nowContent.parent == box){
				box.removeChild(nowContent);
			}
			box.addChild(DisplayObject(_contents[index]));
			if(tabHeight > _paddingTop){
				paddingTop = tabHeight;
			}
			if (isAutoResetXY == true){
				DisplayObject(_contents[index]).x = _padding;
				DisplayObject(_contents[index]).y = _padding;
			}
			nowContent = DisplayObject(_contents[index]);
			if (isAutoClip == true){
				box.scrollRect = new Rectangle(0, 0, _compoWidth - _padding*2, _compoHeight - _paddingTop);
			}
			Nav(navs[index]).setSelectOn();
		}
		
		protected function switchToTabClick(event:MouseEvent):void {
			if (Nav(event.currentTarget.parent).isAble == false)
				return;
			switchTab(uint(Nav(event.currentTarget.parent).index));
		}
		
		/**
		 * 切换到指定索引的标签页
		 * @param newIndex 从0开始的索引
		 */
		public function switchTab(newIndex:uint):void {
			if (Nav(navs[index]) != null){
				Nav(navs[index]).setSelectOff();
				Nav(navs[index]).y = 0;
			}
			index = newIndex;
			switchFun = switchTabMain;
			alphaSwitch();
			this.mouseChildren = false;
			setTimeout(ableClick, ableClickAfterTime);
		}
		
		protected function switchTabMain():void {
			viewContentByIndex(index);
			this.dispatchEvent(new TabBarEvent(TabBarEvent.TAB_SWITCH, index));
		}
		
		protected function alphaSwitch():void {
			if (hn >= 1){
				switchFun();
				return;
			}
			isHide = true;
			alphaShow();
			this.addEventListener(Event.ENTER_FRAME, alphaShow);
		}
		
		override public function setSize(newWidth:Number, newHeight:Number):void {
			super.setSize(newWidth, newHeight);
			if (isAutoClip == true){
				box.scrollRect = new Rectangle(0, 0, _compoWidth - _padding*2, _compoHeight - _paddingTop - 8);
			}

			if (navs.length > 0){
				alignTabs();
			}
		}
		
		/**
		 * 重新排列 
		 */		
		protected function alignTabs():void {
			var tabWidth:Number = _compoWidth/navs.length;
			var sp:int = 0;
			for (var i:int = 0; i < navs.length; i++){
				Nav(navs[i]).getTabSkin(navs.length,tabWidth);
				Nav(navs[i]).setSize(tabWidth, tabHeight);
				if(i==0){
					Nav(navs[i]).x = 0;
					viewContentByIndex(0);			//选中状态
				}else{
					Nav(navs[i]).x = Nav(navs[i - 1]).x + Nav(navs[i - 1]).width;
				}
			}
		}
		
		protected function alphaShow(event:Event = null):void {
			if (isHide == true){
				box.alpha -= hn;
				if (box.alpha <= 0){
					isHide = false;
					switchFun();
					alphaShow(event);
				}
			} else {
				box.alpha += sn;
				if (box.alpha >= 1){
					this.removeEventListener(Event.ENTER_FRAME, alphaShow);
				}
			}
		}
	}
}